package com.cgx.tablet.beeper.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cgx.tablet.beeper.R;
import com.cgx.tablet.beeper.utils.ToastUtil;
import com.yatoooon.screenadaptation.ScreenAdapterTools;

import junit.framework.Test;

/**
 * Created by Administrator on 20/9/15 0015.
 */

public class IPEditText extends LinearLayout {

    private EditText firstIPEdit;
    private EditText secondIPEdit;
    private EditText thirdIPEdit;
    private EditText fourthIPEdit;

    private String firstIP = "";
    private String secondIP = "";
    private String thirdIP = "";
    private String fourthIP = "";

    public IPEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        View view = LayoutInflater.from(context).inflate(R.layout.ip_edittext, this);
        ScreenAdapterTools.getInstance().loadView(view);
        firstIPEdit = (EditText) view.findViewById(R.id.firstIPfield);
        secondIPEdit = (EditText) view.findViewById(R.id.secondIPfield);
        thirdIPEdit = (EditText) view.findViewById(R.id.thirdIPfield);
        fourthIPEdit = (EditText) view.findViewById(R.id.fourthIPfield);

        setIPEditTextListener(context);
    }

    public void setIPEditTextListener(final Context context) {
        //设置第一个字段的事件监听
        firstIPEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                if (null != s) {
                    if (s.length() > 2 || s.toString().trim().contains(".")) {
                        if (s.toString().trim().contains(".")) {
                            firstIP = s.toString().trim().substring(0, s.length() - 1);
                        } else {
                            firstIP = s.toString().trim();
                        }
                        if (Integer.parseInt(firstIP) > 255) {
                            ToastUtil.showShort(context, R.string.limit_ip);
                            return;
                        }
                        secondIPEdit.setFocusable(true);
                        secondIPEdit.requestFocus();
                    } else {
                        firstIP = s.toString().trim();
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                firstIPEdit.removeTextChangedListener(this);
                firstIPEdit.setText(firstIP);
                firstIPEdit.setSelection(firstIP.length());
                firstIPEdit.addTextChangedListener(this);
            }
        });
        firstIPEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_NEXT) {
                    secondIPEdit.setFocusable(true);
                    secondIPEdit.requestFocus();
                }
                return true;
            }
        });
        //设置第二个IP字段的事件监听
        secondIPEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                if (null != s) {
                    if (s.length() > 2 || s.toString().trim().contains(".")) {
                        if (s.toString().trim().contains(".")) {
                            secondIP = s.toString().trim().substring(0, s.length() - 1);

                        } else {
                            secondIP = s.toString().trim();
                        }
                        if (Integer.parseInt(secondIP) > 255) {
                            ToastUtil.showShort(context, R.string.limit_ip);
                            return;
                        }
                        thirdIPEdit.setFocusable(true);
                        thirdIPEdit.requestFocus();
                    } else {
                        secondIP = s.toString().trim();
                    }

                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                secondIPEdit.removeTextChangedListener(this);
                secondIPEdit.setText(secondIP);
                secondIPEdit.setSelection(secondIP.length());
                secondIPEdit.addTextChangedListener(this);
            }
        });
        secondIPEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_NEXT) {
                    thirdIPEdit.setFocusable(true);
                    thirdIPEdit.requestFocus();
                }
                return true;
            }
        });
        //设置第三个IP字段的事件监听
        thirdIPEdit.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

                if (null != s) {
                    if (s.length() > 2 || s.toString().trim().contains(".")) {
                        if (s.toString().trim().contains(".")) {
                            thirdIP = s.toString().trim().substring(0, s.length() - 1);

                        } else {
                            thirdIP = s.toString().trim();
                        }
                        if (Integer.parseInt(thirdIP) > 255) {
                            ToastUtil.showShort(context, R.string.limit_ip);
                            return;
                        }
                        fourthIPEdit.setFocusable(true);
                        fourthIPEdit.requestFocus();
                    } else {
                        thirdIP = s.toString().trim();
                    }

                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                thirdIPEdit.removeTextChangedListener(this);
                thirdIPEdit.setText(thirdIP);
                thirdIPEdit.setSelection(thirdIP.length());
                thirdIPEdit.addTextChangedListener(this);

            }
        });
        thirdIPEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_NEXT) {
                    fourthIPEdit.setFocusable(true);
                    fourthIPEdit.requestFocus();
                }
                return true;
            }
        });
        //设置第四个IP字段的事件监听
        fourthIPEdit.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                if (null != s) {
                    if (s.length() > 2 || s.toString().trim().contains(".")) {
                        if (s.toString().trim().contains(".")) {
                            fourthIP = s.toString().trim().substring(0, s.length() - 1);

                        } else {
                            fourthIP = s.toString().trim();
                        }
                        if (Integer.parseInt(fourthIP) > 255) {
                            ToastUtil.showShort(context, R.string.limit_ip);
                        }
                    } else {
                        fourthIP = s.toString().trim();
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
    }

    public String getText(Context context) {
        if (TextUtils.isEmpty(firstIP) || TextUtils.isEmpty(secondIP)
                || TextUtils.isEmpty(thirdIP) || TextUtils.isEmpty(fourthIP)) {
            ToastUtil.showShort(context, R.string.full_ip);
            return "";
        }
        return firstIP + "." + secondIP + "." + thirdIP + "." + fourthIP;
    }
}
